/*蓝优*/
$('textarea').numberedtextarea();
var current_json = '';
var current_json_str = '';
var xml_flag = false;
var zip_flag = false;
var compress_flag = false;
var isXml = false;

function init() {
	xml_flag = false;
	zip_flag = false;
	compress_flag = false;
	isXml = false;
	$('.xml').attr('style', 'color:#999;');
	$('.zip').attr('style', 'color:#999;');

}
$('#cancelZY').click(function() {
	init();
	var content = $.trim($('#json-src').val());
	var result = '';
	if (content != '') {
		//如果是xml,那么转换为json
		if (content.substr(0, 1) === '<' && content.substr(-1, 1) === '>') {
			isXml = true;
			try {
				var json_obj = $.xml2json(content);
				content = JSON.stringify(json_obj);
			} catch (e) {
				result = '解析错误：<span style="color: #f1592a;font-weight:bold;">' + e.message + '</span>';
				current_json_str = result;
				$('#json-target').html(result);
				return false;
			}
		}
		try {
			var hasZY = $('#cancelZY').prop('checked');
			if (hasZY) {
				content = content.replace(/\\/g, '\\\\');
				content = content.replace(/\\"/g, '\\\\"');
			}
			current_json = jsonlint.parse(content);
			current_json_str = JSON.stringify(current_json);
			result = new JSONFormat(content, 4).toString();
		} catch (e) {
			result = '<span style="color: #f1592a;font-weight:bold;">' + e + '</span>';
			current_json_str = result;
		}
		$('#json-target').html(result);
	} else {
		$('#json-target').html('');
	}
});
$('#json-src').keyup(function() {
	init();
	var content = $.trim($(this).val());
	var result = '';
	if (content != '') {
		//如果是xml,那么转换为json
		if (content.substr(0, 1) === '<' && content.substr(-1, 1) === '>') {
			try {
				var json_obj = $.xml2json(content);
				content = JSON.stringify(json_obj);
			} catch (e) {
				result = '解析错误：<span style="color: #f1592a;font-weight:bold;">' + e.message + '</span>';
				current_json_str = result;
				$('#json-target').html(result);
				return false;
			}
		}
		try {
			var hasZY = $('#cancelZY').prop('checked');
			if (hasZY) {
				content = content.replace(/\\/g, '\\\\');
				content = content.replace(/\\"/g, '\\\\"');
			}
			current_json = jsonlint.parse(content);
			current_json_str = JSON.stringify(current_json);
			result = new JSONFormat(content, 4).toString();
		} catch (e) {
			result = '<span style="color: #f1592a;font-weight:bold;">' + e + '</span>';
			current_json_str = result;
		}
		$('#json-target').html(result);
	} else {
		$('#json-target').html('');
	}

});
$('#right-box').keyup(function() {
	var text = $('#json-target').html().replace(/<br\/>/g, "\n").replace(/<br>/g, "\n").replace(/<[^>]+>/g, "").replace(/&nbsp;/ig, " ").replace(/Object{...}/ig, "").replace(/Array\[[0-9]+\]/ig, "");
	if (isXml) {
		$('#json-src').val($.json2xml(JSON.parse(text)))
	} else {
		$('#json-src').val(text)
	}
});
$('.xml').click(function() {
	if (xml_flag) {
		$('#json-src').keyup();
	} else {
		var result = $.json2xml(current_json);
		$('#json-target').html('<textarea style="width:100%;position:absolute;height: 80vh;min-height:480px;border:0;resize:none;">' + result + '</textarea>');
		xml_flag = true;
		$(this).attr('style', 'color:#15b374;');
	}
});
$('.zip').click(function() {
	if (zip_flag) {
		$('#json-src').keyup();
	} else {
		$('#json-target').html("<xmp>" + current_json_str + "</xmp>");
		zip_flag = true;
		$(this).attr('style', 'color:#15b374;');
	}
});
$('.compress').click(function() {
	if (!compress_flag) {
		$(this).attr('style', 'color:#15b374;');
		$($(".fa-minus-square-o").toArray().reverse()).click();
		compress_flag = true;
	} else {
		while ($(".fa-plus-square-o").length > 0) {
			$(".fa-plus-square-o").click();
		}
		compress_flag = false;
		$(this).attr('style', 'color:#555;');
	}
});
$('.clear').click(function() {
	$('#json-src').val('');
	$('#json-target').html('');
});
(function($) {
	$.fn.innerText = function(msg) {
		if (msg) {
			if (document.body.innerText) {
				for (var i in this) {
					this[i].innerText = msg;
				}
			} else {
				for (var i in this) {
					this[i].innerHTML.replace(/&amp;lt;br&amp;gt;/gi, "n").replace(/(&amp;lt;([^&amp;gt;]+)&amp;gt;)/gi, "");
				}
			}
			return this;
		} else {
			if (document.body.innerText) {
				return this[0].innerText;
			} else {
				return this[0].innerHTML.replace(/&amp;lt;br&amp;gt;/gi, "n").replace(/(&amp;lt;([^&amp;gt;]+)&amp;gt;)/gi, "");
			}
		}
	};
})(jQuery);
var clipboard = new Clipboard('.copy');
$('#json-src').keyup();